using System;
using System.Reflection;
using SwinGame;
using System.Runtime.InteropServices;
using Color = System.Drawing.Color;

namespace SimpleSurvival.src
{
    //-----------------------------------------------//
    //CLASS - GameMain.cs
    //-----------------------------------------------//
    //Entry point for the game
    public class GameMain
    {
        public const int SCREEN_WIDTH = 800;   //Next iteration - externalise
        public const int SCREEN_HEIGHT = 600;
        public const int MAX_FRAME_RATE = 200;
        public const bool LIMIT_FRAME_RATE = true;
        

        public static void Main()
        {
            Timer frameRateCounter = new Timer();
            frameRateCounter.Start();

            //Start the audio system so sound can be played
            Audio.OpenAudio();
            //Open the game window
            Graphics.OpenGraphicsWindow("GameMain", SCREEN_WIDTH,
                SCREEN_HEIGHT);

            Graphics.ShowSwinGameSplashScreen();
            Game game = new Game();


            //Run the game loop
            while (false == Input.WindowCloseRequested())
            {
                double dt =  frameRateCounter.Ticks/1000.0;
                frameRateCounter.Reset();

                //Fetch the next batch of UI interaction
                Input.ProcessEvents();
                game.Update(dt);

                //Clear the screen and draw the framerate
                Graphics.ClearScreen(Color.White);
                game.Draw(dt);
                Text.DrawFramerate(0, 0);

                //Draw onto the screen
                Graphics.RefreshScreen();

                if (LIMIT_FRAME_RATE)
                {
                    while ((double)frameRateCounter.Ticks < //Limit Framerate
                        1000.0 / (double)(MAX_FRAME_RATE + 2.6)) { }
                }
            }

            //End the audio
            Audio.CloseAudio();

            //Close any resources we were using
            Resources.ReleaseAllResources();
        }
    }
}